<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of AutorPaperDAO
 *
 * @author cshepherd
 */
class AutorPaperDAO extends DAO {

    public function select($fields='*', $condition='TRUE', $page='1', $rows_per_page='20', $order_by='id_autor_paper', $order_by_type='DESC') {
        $list_autor_paper = array();
        $data_autor_paper = array();
        try {
            $rows = 0;
            $start = intval(($page-1)*$rows_per_page);
            $query = "SELECT COUNT(id_autor_paper) AS rows FROM autor_paper";
            $query .= " WHERE ".$condition;
            $stmt = $this->dbh->query($query);
            $rows = $stmt->fetchObject()->rows;
            $query = 'SELECT '. $fields .' FROM autor_paper';
            $query .= ' WHERE '.$condition.' ';
            $pages = ceil($rows/$rows_per_page);
            $query .= 'ORDER BY '.$order_by;
            $query .= ' '.$order_by_type;
            $stmt->closeCursor();
            $query .= ' LIMIT '.$start.",".$rows_per_page;
            //echo $query;
            $stmt = $this->dbh->prepare($query);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $result = $stmt->fetchAll();
            foreach($result as $row) {
                $objAutorPaperTO = new AutorPaperTO;
                $objAutorPaperTO->setIdAutor($row->id_autor);
                $objAutorPaperTO->setIdPaper($row->id_paper);
                $list_autor_paper[count($list_autor_paper)+1] = $objAutorPaperTO;
            }
            $data_autor_paper['list_autor_paper']=$list_autor_paper;
            $data_autor_paper['pages']=$pages;
            $data_autor_paper['rows']=$rows;

            return $data_autor_paper;

        }catch(Exception $e) {
            throw $e;
        }
    }

    protected function insert($Entidad) {
        try {
            $id_autor = $Entidad->getIdAutor();
            $id_paper = $Entidad->getIdPaper();


            $query = 'INSERT INTO autor_paper (id_autor, id_paper) ';
            $query .= 'VALUES(:id_autor, :id_paper)';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_autor', $id_autor);
            $stmt->bindParam(':id_paper', $id_paper);
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            if($stmt->execute()) {
                return $this->dbh->lastInsertId();
            }
            else {
                return FALSE;
            }
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function load($objAutorPaperTO) {
        try {
            $query = 'SELECT * FROM autor_paper WHERE id_paper = :id_paper LIMIT 1';
            $stmt = $this->dbh->prepare($query);
            $stmt->bindParam(':id_paper', $objAutorPaperTO->getIdPaper());
            $stmt->setFetchMode(PDO::FETCH_OBJ);
            $stmt->execute();
            $resultado = $stmt->fetch();
            $objAutorPaperTO->setIdAutor($resultado->id_autor);
            return $objAutorPaperTO;
        }
        catch(Exception $e) {
            throw $e;
        }
    }

    public function delete($id) {
        return "";
    }

    protected function update($objetoTO) {
        return "";
    }
}
?>
